<?php
declare (strict_types = 1);

namespace app\adminapi\controller;

use app\common\model\Admin;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;

use think\Request;

class Login
{
public function index(){
    //重置数据库用户密码
    //return encrypt_password(123456);
    $username = \request()->post('username');
    $password = \request()->post('password');
    $admin = Admin::where('username',$username)->find();
    if (!$admin){
        return error(2001,'用户名不存在');
    }elseif (encrypt_password($password) != $admin['password']){
        return error(2001,'密码不正确');
    }else{
        //生成jwt token
        $key = '2006';
        $payload = [
            'iss' => 'http://adminapi.pyg.com',
            'aud' => 'http://www.pyg.com',
            'iat' => time(),
            'nbf' => time(),
        ];

        /**
         * IMPORTANT:
         * You must specify supported algorithms for your application. See
         * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
         * for a list of spec-compliant algorithms.
         */
        $jwt = JWT::encode($payload, $key, 'HS256');
        return success(200,'success',['token'=>$jwt]);
    }
    }


}
